package cn.lysj.framework.verify.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.utils.random.RandomUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.verify.base.VerifyContent;
import cn.lysj.framework.verify.domain.VerifyPhone;
import cn.lysj.framework.verify.repository.VerifyPhoneRepository;
import cn.lysj.framework.verify.util.SmsWLWXUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;

/**
 * @author zwj
 * @description 手机验证表服务层
 * @create 2019-01-15 11:12:57
 **/
@Slf4j
@Service
@Transactional
public class VerifyPhoneService extends VerifyContent implements BaseService<VerifyPhone> {

    @Resource
    private VerifyPhoneRepository verifyPhoneRepository;

    @Override
    public VerifyPhoneRepository getRepository() {
        return verifyPhoneRepository;
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/15 11:17
     * @title phoneVerify
     * @param phone
     * @param verifyCode
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 手机校验验证码
     */
    public Resp phoneVerify(String phone,String verifyCode){
        if (ParamUtil.isBlank(phone)){
            return new Resp(Resp.PARAM_ERROR,"请输入手机号!");
        }
        if (ParamUtil.isBlank(verifyCode)) {
            return new Resp(Resp.PARAM_ERROR,"验证码不能为空!");
        }
        VerifyPhone verifyQuery = verifyPhoneRepository.findByPhone(phone);
        if (verifyQuery == null) {
            return new Resp(Resp.PARAM_ERROR,"验证码错误!");
        }
        if (!verifyCode.equalsIgnoreCase(verifyQuery.getVerifyCode())) {
            return new Resp(Resp.PARAM_ERROR,"验证码错误!");
        }
        if (verifyQuery.getExpires() < System.currentTimeMillis()) {
            return new Resp(Resp.PARAM_ERROR,"验证码已失效!");
        }
        return new Resp("验证通过!");

    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/15 11:32
     * @title sendVerifyCode
     * @param phone
     * @param areaCode
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 发送手机验证码
     */
    public Resp sendVerifyCode(String phone,String areaCode){
        if (ParamUtil.isBlank(phone)) {
            return new Resp(Resp.PARAM_ERROR, "请填写手机号");
        }
        VerifyPhone verifyQuery = verifyPhoneRepository.findByPhone(phone);
        // 若存在历史记录且还在拦截器内,则拦截
        if (null != verifyQuery && verifyQuery.getNextSendPhoneMs() > System.currentTimeMillis()) {
            return new Resp(Resp.PARAM_ERROR, "您的操作太频繁,请稍后再进行操作");
        }
        if(verifyQuery == null){
            verifyQuery = new VerifyPhone();
        }
        verifyQuery.setPhone(phone);
        verifyQuery.setExpires(System.currentTimeMillis() + PHONE_EXPIRES);
        verifyQuery.setNextSendPhoneMs(System.currentTimeMillis() + NEXT_SEND_PHONE);
        verifyQuery.setVerifyCode(RandomUtil.getRandomNum(6));
        boolean flag = SmsWLWXUtil.sendSms("【来亿单】您的验证码为："+verifyQuery.getVerifyCode()+"，请在15分钟内填写",phone);
        if (!flag) {
            return new Resp(Resp.PARAM_ERROR, "短信发送失败,请重试");
        }
        verifyPhoneRepository.save(verifyQuery);
        return new Resp("验证码发送成功");
    }
}
